﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.KJBG;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;

public partial class AddPM : System.Web.UI.Page
{
    /// <summary>
    /// 科技报告操作对象
    /// </summary>
    private KJBG_DAO kjbgDAO = new KJBG_DAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {
        
        String message = null;
        //获取分类编码
        String flbm = Request["flbm"];
        //年份
        String nf = Request["nf"];


        KJBG_FL kjbgFL = null;
        //验证年份
        if (null == nf || nf.Length != 4)
        {
            message = "错误：年份信息有误";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        if (null == flbm || "".Equals(flbm))
        {
            message = "错误：分类编码为空";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        else
        {
            kjbgFL = kjbgDAO.GetKJBG_FLByFlbm(flbm);
            if (null == kjbgFL)
            {
                message = "错误：没有找到对应的分类编码";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return;
            }
        }

        //设置返回按钮链接
        this.backHyperLink.NavigateUrl = GlobalKJBG.LISTPM_PAGE + "?catalog=" + flbm;

        KJBG_PM kjbgPM = this.kjbgDAO.getMaxKJBG_PM(flbm, nf);
       
        
        //分类编码
        this.flbmTextBox.Text = kjbgFL.flbm;
        //分类名称
        this.flmcTextBox.Text = kjbgFL.flmc;
        //索引编码
        this.sybmTextBox.Text=flbm+'-'+nf;

        //年份
        this.nfTextBox.Text = nf;

        //报告编号
        this.bgbhTextBox.Text = "";
        //报告名称
        this.bgmcTextBox.Text ="";
        //报告单位
        this.bgdwTextBox.Text = "";
 
        //作者
        this.zzTextBox.Text = "";

        //中文文摘
        this.zwwzTextBox.Text = "";
        //中文主题词
        this.zwztcTextBox.Text = "";
        //资料来源
        this.zllyTextBox.Text = "";
        //开放
        this.kfCheckBox.Checked = false;
        //获取文件名信息,不包括后缀
        String wjm = null;
        if (null == kjbgPM)
        {
            wjm = GetWJM(null);
        }
        else
        {
            wjm = GetWJM(kjbgPM.wjm);
        }
        //文件名
        this.wjmTextBox.Text = wjm ;
        //页数
        this.ysHiddenField.Value = "";
        User user = (User)Session["login"];
        if (null == user)
        {
            GlobalMethod.RedirectLogin(Response);
        }
        else
        {
            this.dlrTextBox.Text = user.name;
        }
    }

    /// <summary>
    /// 获取用户填写的科技报告告篇名信息
    /// </summary>
    /// <returns></returns>
    private KJBG_PM GetKJBGPM()
    {
        KJBG_PM kjbgPM = new KJBG_PM();
        //索引编码
        kjbgPM.sybm = this.sybmTextBox.Text;
        //分类编码
        kjbgPM.flbm = this.flbmTextBox.Text;
        //文件名
        kjbgPM.wjm = this.wjmTextBox.Text;
        //报告编号
        kjbgPM.bgbh = this.bgbhTextBox.Text.Trim();
        //年份
        kjbgPM.nf = Int32.Parse(this.nfTextBox.Text);
        //报告名称
        kjbgPM.bgmc = this.bgmcTextBox.Text.Trim();
        //报告单位
        kjbgPM.bgdw = this.bgdwTextBox.Text.Trim();
        //作者
        kjbgPM.zz = this.zzTextBox.Text.Trim();
        //中文文摘
        kjbgPM.zwwz = this.zwwzTextBox.Text.Trim();
        //中文主题词
        kjbgPM.zwztc = this.zwztcTextBox.Text.Trim();
        //页数
        kjbgPM.ys =Int32.Parse(this.ysHiddenField.Value) ;
       
        //登录人
        kjbgPM.dlr =this.dlrTextBox.Text;
       
        //更新日期
        kjbgPM.gxrq = DateTime.Now;
       
        //资料来源
        kjbgPM.zlly = this.zllyTextBox.Text;
        //开放
        kjbgPM.kf = this.kfCheckBox.Checked;
        return kjbgPM;

    }

    /// <summary>
    /// 获取下一个文件名，包括后缀
    /// </summary>
    /// <param name="wjm"></param>
    /// <returns></returns>
    private String GetWJM(String wjm)
    {
        String result = null;
        if (null == wjm || "".Equals(wjm))
        {
            result = this.sybmTextBox.Text + "-000.PDF";
        }
        else
        {
            //获取文件名的后三位流水号，并在其基础上加1
            int endIndex = wjm.LastIndexOf(".");

            String phString = wjm.Substring(endIndex - 3, 3);

            int ph = 0;

            try
            {
                ph = Int32.Parse(phString);
            }
            catch (Exception)
            {
            }

            //当前篇号+1
            ph += 1;

            phString = ph.ToString();
            //长度为1
            if (phString.Length == 1)
            {
                phString = "00" + phString;
            }
            else if (phString.Length == 2)
            {
                phString = "0" + phString;
            }
            else if (phString.Length >3)
            {
                String message = "错误：篇名数量已经超过了1000篇";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return null;
            }

            result = this.sybmTextBox.Text + "-" + phString+".PDF";
        }
        return result;
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (!this.ValidateBGMC())
        {
            return;
        }
        if (!this.ValidateNF())
        {
            return;
        }
        if (!this.ValidateWJM())
        {
            return;
        }
        
        if (!ValidateYWJMFileUpload())
        {
            return;
        }
        KJBG_PM kjbgPM = this.GetKJBGPM();
        if (OperationFile.Upload(this.ywjmFileUpload, kjbgPM.wjm))
        {
            if (this.kjbgDAO.InsertKJBG_PM(kjbgPM))
            {
                Response.Write("<Script>alert('添加考察报告篇名信息成功')</Script>");
                this.BindData();
            }
        }
        else
        {
            Response.Write("<Script>alert('添加考察报告篇名信息失败')</Script>");
        }
    }

    /// <summary>
    /// 验证文件名
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateWJM()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        KJBG_PM kjbgPM = this.kjbgDAO.GetKJBG_PMByWjm(this.wjmTextBox.Text);
        if (null != kjbgPM)
        {
            this.errorLabel.Text = "错误：该文件名已存在";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证报告名称
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateBGMC()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if (null == this.bgmcTextBox.Text || "".Equals(this.bgmcTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：报告名称不能为空";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证年份
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateNF()
    {
        Boolean flag = true;

        this.nfTextBox.Text=this.nfTextBox.Text.Trim();
        this.errorLabel.Text = "";
        if ("".Equals(this.nfTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：年份不能为空";
            flag = false;
        }
        else
        {
            if (nfTextBox.Text.Length != 4)
            {
                this.errorLabel.Text = "错误：请输入正确的年份，年份区间1000~9999";
                flag = false;
            }
            else
            {
                try
                {
                    Int32.Parse(this.nfTextBox.Text);
                }
                catch (Exception)
                {
                    this.errorLabel.Text = "错误：请输入正确的年份";
                    flag = false;
                }
            }
        }
        return flag;
    }

    /// <summary>
    /// 验证上传文件
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateYWJMFileUpload()
    {
        this.errorLabel.Text = "";
        Boolean flag = true;
        if (null == this.ywjmFileUpload || !this.ywjmFileUpload.HasFile)
        {
            this.errorLabel.Text = "错误：请选择上传的文件";
            flag = false;
        }
        else
        {
            if (!this.ywjmFileUpload.FileName.EndsWith(".PDF", true, null))
            {
                this.errorLabel.Text = "错误：请选择PDF类型文件";
                flag = false;
            }
            else
            {
                PdfReader pdf = new PdfReader(this.ywjmFileUpload.FileBytes);
                if (null != pdf)
                {
                    //文件大小不能超过10M
                    if (pdf.FileLength >= 10 * 1024 * 1024)
                    {
                        this.errorLabel.Text = "错误：选择的文件大小超过了10M";
                        pdf.Close();
                        return false;
                    }

                    this.ysHiddenField.Value = pdf.NumberOfPages.ToString();
                }

                pdf.Close();
            }
        }
        return flag;
    }

    protected void bgdwTextBox_TextChanged(object sender, EventArgs e)
    {
        this.bgdwTextBox.Text = GlobalMethod.Semicolon(this.bgdwTextBox.Text);
    }
    protected void zzTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zzTextBox.Text = GlobalMethod.Semicolon(this.zzTextBox.Text);
    }
    protected void zwztcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zwztcTextBox.Text = GlobalMethod.Semicolon(this.zwztcTextBox.Text);
    }
}